package com.fhnew.app.ui.activity.userinfo.fragment.yue;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.model.internet.HttpAPI;
import com.fhnew.app.model.internet.HttpRequest;
import com.fhnew.app.model.internet.been.B_InTuE01;
import com.fhnew.app.model.internet.been.B_OutYuE1;
import com.fhnew.app.model.internet.been.Been;
import com.fhnew.app.ui.activity.userinfo.FragmentDataChang;
import com.fhnew.app.ui.activity.userinfo.PageDataChangeControl;
import com.fhnew.app.util.Md5;
import com.fhnew.app.widget.CustomEditText;
import com.fhnew.app.widget.LoadingDialog;


public class ChargeDialogFragment implements FragmentDataChang {

    private PageDataChangeControl pageDataChangeControl;
    private ViewGroup rootView;
    private Context context;
    private Bundle bundle;

    public ChargeDialogFragment (ViewGroup rootView){
        this.rootView =  rootView;
        this.context = rootView.getContext();
        initView(rootView);
    }

    public Context getContext(){
        return context;
    }

    @Override
    public void setListener (PageDataChangeControl pageDataChangeControl) {
        this.pageDataChangeControl = pageDataChangeControl;
    }

    @Override
    public void dataChanged (Bundle bundle) {
        this.bundle = bundle;
        price = bundle.getInt("price");
        type = bundle.getString("type");
        game_id = bundle.getInt("game_id");
        token = bundle.getString("token");
        game_Name = bundle.getString("name");
        initInfoView();
    }

    public void dismissDialog(boolean status){
        bundle.putBoolean("isBack",true);
        bundle.putBoolean("finish",status);
        pageDataChangeControl.changeFragmemt("Back",bundle);
    }

    //=====================================================================
    // View
    //=====================================================================


    private View coverTouch;

    private int price;
    private String type;
    private int game_id;
    private String game_Name;
    private String token;

    private ViewGroup firstLayout;
    private Button f_cancel, f_accept;
    private ImageView close;

    private ViewGroup passwordLayout;
    private TextView passwordTips, passwordTitle;
    private CustomEditText password;
//    private EditText password01, password02, password03, password04;
    private Button passwordAccept;

    private TextView title;
    private TextView tips01,tips;
    private TextView count;

    private LoadingDialog loadingDialog;

    private void initView (View view) {

        coverTouch = view.findViewById(R.id.view_YuE_CoverTouch);
        coverTouch.setOnClickListener(onClickListener);

        loadingDialog = new LoadingDialog((ViewGroup) view);
        loadingDialog.setShowTips(false);
        loadingDialog.setDialogCancelable(false);

        title = view.findViewById(R.id.titletext);
        tips01 = view.findViewById(R.id.textView_YuE_Password_Tips01);
        tips = view.findViewById(R.id.textView_YuE_Password_Tips);

        count = view.findViewById(R.id.textView_YuE_Count);

        firstLayout = view.findViewById(R.id.relativeLayout_YuE_First);
        f_cancel = view.findViewById(R.id.button__YuE_First_Cancel);
        f_accept = view.findViewById(R.id.button__YuE_First_Accept);
        f_cancel.setOnClickListener(onClickListener);
        f_accept.setOnClickListener(onClickListener);
        close = view.findViewById(R.id.close);
        close.setOnClickListener(onClickListener);

        passwordLayout = view.findViewById(R.id.relativeLayout_YuE_Password);
        passwordTips = view.findViewById(R.id.textView_YuE_Password_Tips);
        passwordTitle = view.findViewById(R.id.textView_YuE_Count);
        password = view.findViewById(R.id.editText_YuE_PassWord);
        passwordAccept = view.findViewById(R.id.button_YuE_P_Accept);
        passwordAccept.setOnClickListener(onClickListener);

    }

    private void initInfoView(){
        if(coverTouch==null || type == null)return;

        password.setText("");

        firstLayout.setVisibility(View.VISIBLE);
        passwordLayout.setVisibility(View.GONE);

        if (type.equals("1")) {
            title.setText("转入");
            tips01.setText("确认将"+game_Name+"余额转入至余额宝？");
            tips.setText("转入"+game_Name+"金额：");
            passwordTips.setText("转入至余额宝");
        } else {
            title.setText("转出");
            tips01.setText("确认将余额宝余额转出至"+game_Name+"？");
            tips.setText("转出"+game_Name+"金额：");
            passwordTips.setText("转出至"+game_Name);
        }
    }

    private void inputPressword () {
        firstLayout.setVisibility(View.GONE);

        passwordLayout.setVisibility(View.VISIBLE);

        title.setText("输入密码");

        count.setText("￥ " + price);
    }

    private void startRequest () {
        String s_password = password.getText().toString();
        if (type.equals("1")) {
            getInYuE.startRequest(token , game_id, price, Md5.toMd5(s_password));
        } else {
            getOutYuE.startRequest(token , game_id, price, Md5.toMd5(s_password));
        }
    }

    private void clearPassword(){
        password.setText("");
    }

    //=============================================================
    // 网络请求
    //=============================================================
    private HttpAPI.GetInYuE getInYuE = new HttpAPI.GetInYuE() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest) {
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_InTuE01 result) {
             if (result.getCode() == 0) {
                if(game_id == 0){
                    Toast.makeText(getContext(), "订单提交成功", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                    dismissDialog(true);
                    clearPassword();
                }else{
                    getInTuE2.startRequest(token, result.getData().getOrderId());
                }
            } else {
                Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
                loadingDialog.dismiss();
                clearPassword();
            }
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "请求失败", Toast.LENGTH_SHORT).show();
            dismissDialog(false);
            loadingDialog.dismiss();
            clearPassword();
        }
    };

    private HttpAPI.GetInTuE2 getInTuE2 = new HttpAPI.GetInTuE2() {
        @Override
        public void result (Been result) {
            if (result.getCode() == 0) {
                Toast.makeText(getContext(), "订单提交成功", Toast.LENGTH_SHORT).show();
                dismissDialog(true);
            } else {
                Toast.makeText(getContext(), "订单："+result.getMsg() + result.getCode(), Toast.LENGTH_SHORT).show();
                dismissDialog(false);
            }
            loadingDialog.dismiss();
            clearPassword();
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "订单提交失败", Toast.LENGTH_SHORT).show();
            dismissDialog(false);
            loadingDialog.dismiss();
            clearPassword();
        }
    };

    private HttpAPI.GetOutYuE getOutYuE = new HttpAPI.GetOutYuE() {

        @Override
        public void onStartedRequest (HttpRequest httpRequest){
            super.onStartedRequest(httpRequest);
            loadingDialog.showLoading();
        }

        @Override
        public void result (B_OutYuE1 result){
            if(result.getCode() == 0){
                if(game_id == 0){
                    Toast.makeText(getContext(), "订单提交成功", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                    dismissDialog(true);
                    clearPassword();
                }else{
                    getOutYuE2.startRequest(token,game_id,result.getData().getOrderId());
                }
            }else{
                Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
                dismissDialog(false);
                clearPassword();
                loadingDialog.dismiss();
            }
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "请求失败", Toast.LENGTH_SHORT).show();
            dismissDialog(false);
            loadingDialog.dismiss();
            clearPassword();
        }
    };

    private HttpAPI.GetOutYuE2 getOutYuE2 = new HttpAPI.GetOutYuE2() {
        @Override
        public void result (Been result) {
            if(result.getCode() == 0){
                Toast.makeText(getContext(), "转出成功", Toast.LENGTH_SHORT).show();
                dismissDialog(true);
                clearPassword();
            }else{
                Toast.makeText(getContext(), "订单："+result.getMsg(), Toast.LENGTH_SHORT).show();
                dismissDialog(false);
                clearPassword();
            }
            loadingDialog.dismiss();
        }

        @Override
        public void error (String msg) {
            Toast.makeText(getContext(), "订单提交失败", Toast.LENGTH_SHORT).show();
            dismissDialog(false);
            loadingDialog.dismiss();
            clearPassword();
        }
    };

    //=============================================================
    // 事件
    //=============================================================
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            switch (v.getId()) {
                case R.id.button__YuE_First_Cancel:
                case R.id.close:
                    dismissDialog(false);
                    break;
                case R.id.button__YuE_First_Accept:
                    inputPressword();
                    break;
                case R.id.button_YuE_P_Accept:
                    startRequest();
                    break;
            }
        }
    };

}
